package com.teradyne.merlin2.ui.client.places;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceHistoryMapper;

//@WithTokenizers({ HomePlace.Tokenizer.class, OrderListPlace.Tokenizer.class,
//		OrderEntryPlace.Tokenizer.class, OrderHistoryPlace.Tokenizer.class,
//		QuoteListPlace.Tokenizer.class, QuoteEntryPlace.Tokenizer.class,
//		QuoteHistoryPlace.Tokenizer.class, WorkListPlace.Tokenizer.class,
//		WorkListActivePlace.Tokenizer.class, WorkListPlace.Tokenizer.class })
//public interface AppPlaceHistoryMapper extends PlaceHistoryMapper {
//}

public class AppPlaceHistoryMapper implements PlaceHistoryMapper {
	String delimiter = "/";

	@Override
	public Place getPlace(String token) {
		String[] tokens = token.split(delimiter);
		if (tokens[0].equals(PlacesPrefixesConstants.HOME)) {
			return new HomePlace();
		} else if (tokens[0].equals(PlacesPrefixesConstants.ORDER)) {
			if (tokens[1].equals(PlacesPrefixesConstants.ORDER_ENTRY)) {
				return new OrderEntryPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.ORDER_HISTORY)) {
				return new OrderHistoryPlace();
			}
			return new OrderListPlace();
		} else if (tokens[0].equals(PlacesPrefixesConstants.QUOTE)) {
			if (tokens[1].equals(PlacesPrefixesConstants.QUOTE_ENTRY)) {
				return new QuoteEntryPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.QUOTE_HISTORY)) {
				return new QuoteHistoryPlace();
			}
			return new QuoteListPlace();
		} else if (tokens[0].equals(PlacesPrefixesConstants.SERVICES)) {
			if (tokens[1].equals(PlacesPrefixesConstants.SERVICES_INSTALLATION_PROBLEM)) {
				return new ServiceInstallProblemPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.SERVICES_ADD_ADDRESS)) {
				return new ServiceAddAddressPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.SERVICES_CHANGE_ADDRESS)) {
				return new ServiceChangeAddressPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.SERVICES_OTHER)) {
				return new ServiceOtherPlace();
			}
			return new ServicestListPlace();
		} else if (tokens[0].equals(PlacesPrefixesConstants.WORKLIST)) {
			if (tokens[1].equals(PlacesPrefixesConstants.WORKLIST_ACTIVE)) {
				return new WorkListActivePlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.WORKLIST_APPROVED)) {
				return new WorkListApprovedPlace();
			} else if (tokens[1].equals(PlacesPrefixesConstants.WORKLIST_CLOSED)) {
				return new WorkListClosedPlace();
			}
			return new WorkListPlace();
		}
		return null;
	}

	@Override
	public String getToken(Place place) {
		if (place instanceof HomePlace) {
			return PlacesPrefixesConstants.HOME + delimiter;
		} else if (place instanceof OrderListPlace) {
			return PlacesPrefixesConstants.ORDER + delimiter + PlacesPrefixesConstants.LIST + delimiter;
		} else if (place instanceof OrderEntryPlace) {
			return PlacesPrefixesConstants.ORDER + delimiter + PlacesPrefixesConstants.ORDER_ENTRY + delimiter;
		} else if (place instanceof OrderHistoryPlace) {
			return PlacesPrefixesConstants.ORDER + delimiter + PlacesPrefixesConstants.ORDER_HISTORY + delimiter;
		}

		else if (place instanceof QuoteListPlace) {
			return PlacesPrefixesConstants.QUOTE + delimiter + PlacesPrefixesConstants.LIST + delimiter;
		} else if (place instanceof QuoteEntryPlace) {
			return PlacesPrefixesConstants.QUOTE + delimiter + PlacesPrefixesConstants.QUOTE_ENTRY + delimiter;
		} else if (place instanceof QuoteHistoryPlace) {
			return PlacesPrefixesConstants.QUOTE + delimiter + PlacesPrefixesConstants.QUOTE_HISTORY + delimiter;
		}

		else if (place instanceof ServicestListPlace) {
			return PlacesPrefixesConstants.SERVICES + delimiter + PlacesPrefixesConstants.LIST + delimiter;
		} else if (place instanceof ServiceInstallProblemPlace) {
			return PlacesPrefixesConstants.SERVICES + delimiter + PlacesPrefixesConstants.SERVICES_INSTALLATION_PROBLEM + delimiter;
		} else if (place instanceof ServiceAddAddressPlace) {
			return PlacesPrefixesConstants.SERVICES + delimiter + PlacesPrefixesConstants.SERVICES_ADD_ADDRESS + delimiter
					+ PlacesPrefixesConstants.SERVICES_ADDRESS + delimiter;
		} else if (place instanceof ServiceChangeAddressPlace) {
			return PlacesPrefixesConstants.SERVICES + delimiter + PlacesPrefixesConstants.SERVICES_CHANGE_ADDRESS + delimiter
					+ PlacesPrefixesConstants.SERVICES_ADDRESS + delimiter;
		} else if (place instanceof ServiceOtherPlace) {
			return PlacesPrefixesConstants.SERVICES + delimiter + PlacesPrefixesConstants.SERVICES_OTHER + delimiter;
		}

		else if (place instanceof WorkListPlace) {
			return PlacesPrefixesConstants.WORKLIST + delimiter + PlacesPrefixesConstants.LIST + delimiter;
		} else if (place instanceof WorkListActivePlace) {
			return PlacesPrefixesConstants.WORKLIST + delimiter + PlacesPrefixesConstants.WORKLIST_ACTIVE + delimiter;
		} else if (place instanceof WorkListApprovedPlace) {
			return PlacesPrefixesConstants.WORKLIST + delimiter + PlacesPrefixesConstants.WORKLIST_APPROVED + delimiter;
		} else if (place instanceof WorkListClosedPlace) {
			return PlacesPrefixesConstants.WORKLIST + delimiter + PlacesPrefixesConstants.WORKLIST_CLOSED + delimiter;
		}
		return null;
	}
}
